package com.ruoyi.receiving.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.SysOrder;
import com.ruoyi.receiving.domain.SysPeelingRecord;
import com.ruoyi.receiving.mapper.SysPeelingRecordMapper;
import com.ruoyi.receiving.service.ISysPeelingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 去皮记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-15
 */
@Service
public class SysPeelingRecordServiceImpl implements ISysPeelingRecordService
{
    @Autowired
    private SysPeelingRecordMapper sysPeelingRecordMapper;

    /**
     * 查询去皮记录
     * 
     * @param id 去皮记录主键
     * @return 去皮记录
     */
    @Override
    public SysPeelingRecord selectSysPeelingRecordById(Long id)
    {
        return sysPeelingRecordMapper.selectSysPeelingRecordById(id);
    }

    /**
     * 查询去皮记录列表
     * 
     * @param sysPeelingRecord 去皮记录
     * @return 去皮记录
     */
    @Override
    public List<SysPeelingRecord> selectSysPeelingRecordList(SysPeelingRecord sysPeelingRecord)
    {
        return sysPeelingRecordMapper.selectSysPeelingRecordList(sysPeelingRecord);
    }

    /**
     * 新增去皮记录
     * 
     * @param sysPeelingRecord 去皮记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysPeelingRecord(SysPeelingRecord sysPeelingRecord)
    {
        sysPeelingRecord.setCreateTime(DateUtils.getNowDate());
        int rows = sysPeelingRecordMapper.insertSysPeelingRecord(sysPeelingRecord);
        insertSysOrder(sysPeelingRecord);
        return rows;
    }

    /**
     * 修改去皮记录
     * 
     * @param sysPeelingRecord 去皮记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysPeelingRecord(SysPeelingRecord sysPeelingRecord)
    {
        sysPeelingRecord.setUpdateTime(DateUtils.getNowDate());
        sysPeelingRecordMapper.deleteSysOrderByOrderCode(sysPeelingRecord.getId());
        insertSysOrder(sysPeelingRecord);
        return sysPeelingRecordMapper.updateSysPeelingRecord(sysPeelingRecord);
    }

    /**
     * 批量删除去皮记录
     * 
     * @param ids 需要删除的去皮记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysPeelingRecordByIds(Long[] ids)
    {
        sysPeelingRecordMapper.deleteSysOrderByOrderCodes(ids);
        return sysPeelingRecordMapper.deleteSysPeelingRecordByIds(ids);
    }

    /**
     * 删除去皮记录信息
     * 
     * @param id 去皮记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysPeelingRecordById(Long id)
    {
        sysPeelingRecordMapper.deleteSysOrderByOrderCode(id);
        return sysPeelingRecordMapper.deleteSysPeelingRecordById(id);
    }

    /**
     * 新增订单记录信息
     * 
     * @param sysPeelingRecord 去皮记录对象
     */
    public void insertSysOrder(SysPeelingRecord sysPeelingRecord)
    {
        List<SysOrder> sysOrderList = sysPeelingRecord.getSysOrderList();
        String orderCode = sysPeelingRecord.getOrderCode();
        if (StringUtils.isNotNull(sysOrderList))
        {
            List<SysOrder> list = new ArrayList<SysOrder>();
            for (SysOrder sysOrder : sysOrderList)
            {
                sysOrder.setOrderCode(orderCode);
                list.add(sysOrder);
            }
            if (list.size() > 0)
            {
                sysPeelingRecordMapper.batchSysOrder(list);
            }
        }
    }
}
